#!/usr/bin/env Rscript
################################################################################
# @ Author       : Pengdong Yan
# @ Email        : yanpd01@snnu.edu.cn
# @ Encoding     : UTF-8
# @ Language     :
# @ Date         : 2022-04-10 20:18
# @ LastEditTime : 2022-04-10 23:57
# @ Description  :
################################################################################

rm(list = ls())
library(rstatix)
library(yyeasy)
library(extrafont)
library(ggpmisc)
fonts()

group <- yyread("0_data/all_data.xlsx", excel = T, sheet = "编号")
df_agro <- yyread("0_data/all_data.xlsx", excel = T, sheet = "表观")
df_swj <- yyread("0_data/all_data.xlsx", excel = T, sheet = "生物碱")
df_poly <- yyread("0_data/all_data.xlsx", excel = T, sheet = "多糖蛋白")

df_soil1 <- yyread("0_data/all_data.xlsx", excel = T, sheet = "土壤1")
df_soil2 <- yyread("0_data/all_data.xlsx", excel = T, sheet = "土壤2")
df_soil3 <- yyread("0_data/all_data.xlsx", excel = T, sheet = "土壤3")
df_qpcr <- yyread("0_data/all_data.xlsx", excel = T, sheet = "定量")
df_mh <- yyread("0_data/all_data.xlsx", excel = T, sheet = "酶活")


df_agro$id <- factor(df_agro$id, levels = group$id, labels = group$新编号)
df_swj$id <- factor(df_swj$id, levels = group$id, labels = group$新编号)
df_poly$id <- factor(df_poly$id, levels = group$id, labels = group$新编号)

df_soil1$id <- factor(df_soil1$id, levels = group$id, labels = group$新编号)
df_soil2$id <- factor(df_soil2$id, levels = group$id, labels = group$新编号)
df_soil3$id <- factor(df_soil3$id, levels = group$id, labels = group$新编号)
df_qpcr$id <- factor(df_qpcr$id, levels = group$id, labels = group$新编号)
df_mh$id <- factor(df_mh$id, levels = group$id, labels = group$新编号)



tmp_1 <- df_agro %>%
    group_by(id) %>%
    summarise_all(mean)
tmp_2 <- cbind(df_swj, df_poly[, -1]) %>%
    group_by(id) %>%
    summarise_all(mean)
df_all_fz <- cbind(tmp_1, tmp_2[, -1]) %>% column_to_rownames("id")

df_all_soil <- cbind(df_soil1, df_soil2[, -1], df_soil3[, -1], df_qpcr[, -1], df_mh[, -1]) %>%
    group_by(id) %>%
    summarise_all(mean) %>%
    column_to_rownames("id")
colnames(df_all_fz)  <- colnames(df_all_fz) %>% str_remove("（.*")
colnames(df_all_soil) <-  colnames(df_all_soil) %>% str_remove("（.*")

pheat <- function(cor, main = "", adj = "none", cluster_rows = T, cluster_cols = T, rm_1 = F, ...) {
    tmp_k <- seq(-1, 1, 0.02)
    tmp_col <- colorRampPalette(rev(RColorBrewer::brewer.pal(n = 7, name = "RdYlBu")))(length(tmp_k))
    pheatmap::pheatmap(
        cor$r,
        display_numbers = get_corr_sig(cor$r, cor$p, adj, rm_1 = rm_1),
        breaks = tmp_k,
        color = tmp_col,
        cluster_rows = cluster_rows,
        cluster_cols = cluster_cols,
        main = main,
        ...
    )
}

pheat(psych::corr.test(df_all_fz, df_all_soil, adjust = "none"), angle_col = 90, fontfamily = "SimSun", fontsize = 10)
pheat(psych::corr.test(df_all_fz, adjust = "none"), angle_col = 90, fontfamily = "SimSun", fontsize = 10)
pheat(psych::corr.test(df_all_soil, adjust = "none"), angle_col = 90, fontfamily = "SimSun", fontsize = 10,
treeheight_row = 30, treeheight_col = 30)